package tmp;

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.text.DateFormat;
import java.util.Properties;

import LawsonTransformJava.dao.DAOFactoryAccessDB;
import LawsonTransformJava.dao.DAOFactoryOracleDB;
import LawsonTransformJava.dao.DAOOracleDB;

public class DAObject {

	static final String DEFAULT_SEPARATOR = ",";

//	protected DAOFactoryOracleDB data = null;
//	protected DAOFactoryAccessDB xref = null;
	protected DAOOracleDB data = null;
	protected DAOFactoryAccessDB xref = null;

	protected ResultSet rslt = null;
	protected ResultSetMetaData rsmd = null;
	protected ResultSet countRecs = null;

	protected Statement stmtData = null;
	protected Connection connData = null;
	protected Statement countStmtData = null;
	protected String sqlData = null;

//	protected Statement stmtOra = null;
//	protected Connection connectionOra = null;
//	protected Statement countStmtOra = null;

	protected DateFormat df = null;
	protected boolean hasMoreRecs = true;
	protected int recCount = 0;
	protected String whereClause = "";
	protected FileWriter output = null;
	protected File outputFile = null;	

	 public Object createObject(ResultSet rslt, Object obj) throws SQLException, InvocationTargetException, IllegalAccessException,
		IllegalArgumentException {
		//obj.getClass().cast(arg0);
		//Employee emp = new Employee();
		Method[] mthds = obj.getClass().getMethods();
		int i=0;
		String str1 = null;
		int columnCount = rsmd.getColumnCount();
		for(i=0;i<columnCount;i++) {
			if (rsmd.getColumnTypeName(i+1)=="CHAR") {
				int j=0;
				for(j=0;j<mthds.length;j++) {
					if (mthds[j].getName().equalsIgnoreCase("set" + rsmd.getColumnName(i+1)))
					{
						if ((str1=rslt.getString(i+1))!=null) {
//							mthds[j].invoke(obj, "\"" + rslt.getString(i+1) + "\"");
							mthds[j].invoke(obj, str1.trim());
							//Get out of loop
							j=mthds.length+1;
						}
					}
				}
			}
			else if(rsmd.getColumnTypeName(i+1)=="NUMBER")
				if(rsmd.getScale(i+1)==0) {
					for(int j=0;j<mthds.length;j++) {
						if (mthds[j].getName().equalsIgnoreCase("set" + rsmd.getColumnName(i+1)))
						{
							mthds[j].invoke(obj, rslt.getInt(i+1));
							//Get out of loop
							j=mthds.length+1;
						}
					}
				}
				else
					for(int j=0;j<mthds.length;j++) {
						if (mthds[j].getName().equalsIgnoreCase("set" + rsmd.getColumnName(i+1)))
						{
//							System.out.println(rsmd.getColumnName(i+1));
							mthds[j].invoke(obj, rslt.getFloat(i+1));
							
							//Get out of loop
							j=mthds.length+1;
						}
					}
			else if(rsmd.getColumnTypeName(i+1)=="DATE")
				if(rsmd.getScale(i+1)==0) {
					for(int j=0;j<mthds.length;j++) {
						if (mthds[j].getName().equalsIgnoreCase("set" + rsmd.getColumnName(i+1)))
						{
							mthds[j].invoke(obj, rslt.getDate(i+1));
							//Get out of loop
							j=mthds.length+1;
						}
					}
				}
		}
		/* Find the PAEmployee by Employee Number and set it as the Employee objects pae */
//		obj.setPAEmployee((new OraclePAEmployeeDAO()).findEmployee(Integer.toString(obj.getEmployee())));
		return obj;
}

	
	private String printFieldHeadings(Connection connection, String separator) {
		StringBuffer sb = new StringBuffer();

		try {
		    rslt = stmtData.executeQuery(sqlData);
		    rsmd = rslt.getMetaData();
		    
		    if (separator==null)
		    	separator=",";
		    
		    for (int i=1; i <= rsmd.getColumnCount(); i++)
		    {
		    	sb.append(rsmd.getColumnName(i) + separator);
		    }
			return sb.toString();
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return null;
	}


	public ResultSetMetaData getMetaData() {
		try {
		    rslt = stmtData.executeQuery(sqlData);
		    rsmd = rslt.getMetaData();
			
			return rsmd;
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return null;
	}

	public void setWhereClause(String where) {
		this.whereClause = where.trim();
	}
	
	public String getFieldHeadings() {
		return printFieldHeadings();
	}

	public String printFieldHeadings(String separator) {
		return printFieldHeadings(connData, separator);
	}
	
	public String printFieldHeadings() {
		return printFieldHeadings(connData,DEFAULT_SEPARATOR);
	}
}
